﻿using Common.Contract;
using Common.Contract.Authentication;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.ProductHandbook;
using Common.Contract.ProductHandbook.Record;
using Common.Contract.ProductHandbook.Request;
using Common.Contract.ProductHandbook.Response;
using Common.Contract.SalePerformance;
using Common.Contract.SalePerformance.Record;
using Common.Contract.SalePerformance.Request;
using Common.Contract.SalePerformance.Response;
using Common.Contract.SystemConfig.Record;
using Common.Service;
using Common.Service.DataTables;
using Common.Type;
using SMECustomerWebsite.Core.Configuration;
using SMECustomerWebsite.Filters;
using SMECustomerWebsite.Models.ViewModels.ProductHandbook;
using SMECustomerWebsite.Models.ViewModels.SalePerformance;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web.Mvc;

namespace SMECustomerWebsite.Controllers
{
    public class SalePerformanceController : ControllerBase
    {
        #region Deposit
        [CustomActionFilter]
        [Authorize]
        public ActionResult Deposit(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult DepositData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.DepositData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveDepositByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                             x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9,
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        #endregion
        #region Loan

        [CustomActionFilter]
        [Authorize]
        public ActionResult LoanByProduct(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listMSBO = new JsonResult();
            if (CurrentUserRecord.GroupIdentifier != "4")
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveListMsboRequest request = new RetrieveListMsboRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanByProduct"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 10000,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                    };
                    var customerRecords = new RetrieveListMsboResponse();
                    customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                    var objectItem = customerRecords.Records.Select(x => new
                    {
                        id = x.Dao,
                        text = x.FullName,
                    });
                    listMSBO = Json(objectItem);
                }
            }
            resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer,
                ListMSBO = listMSBO
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult LoanByProductProg(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listMSBO = new JsonResult();
            if (CurrentUserRecord.GroupIdentifier != "4")
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveListMsboRequest request = new RetrieveListMsboRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanByProduct"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 10000,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                    };
                    var customerRecords = new RetrieveListMsboResponse();
                    customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                    var objectItem = customerRecords.Records.Select(x => new
                    {
                        id = x.Dao,
                        text = x.FullName,
                    });
                    listMSBO = Json(objectItem);
                }
            }
            resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer,
                ListMSBO = listMSBO
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult LoanByCtUuDai(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listMSBO = new JsonResult();
            if (CurrentUserRecord.GroupIdentifier != "4")
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveListMsboRequest request = new RetrieveListMsboRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanByCtUuDai"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 10000,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                    };
                    var customerRecords = new RetrieveListMsboResponse();
                    customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                    var objectItem = customerRecords.Records.Select(x => new
                    {
                        id = x.Dao,
                        text = x.FullName,
                    });
                    listMSBO = Json(objectItem);
                }
            }
            resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer,
                ListMSBO = listMSBO
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult LoanData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveLoanProductByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                             x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9,
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult LoanProductProgramData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveLoanProductProgramByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                             x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9,
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult LoanByCtUuDaiData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.LoanByCtUuDaiData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveLoanPromoProgramByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                              x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9,
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Cash management
        [CustomActionFilter]
        [Authorize]
        public ActionResult CashManagement(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listMSBO = new JsonResult();
            if (CurrentUserRecord.GroupIdentifier != "4")
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveListMsboRequest request = new RetrieveListMsboRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.Deposit"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 10000,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                    };
                    var customerRecords = new RetrieveListMsboResponse();
                    customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                    var objectItem = customerRecords.Records.Select(x => new
                    {
                        id = x.Dao,
                        text = x.FullName,
                    });
                    listMSBO = Json(objectItem);
                }
            }
            resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer,
                ListMSBO = listMSBO
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult CashManagementData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.CashManagementData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveCashManagementByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                             x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9, 
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        #endregion

        #region Trade
        [CustomActionFilter]
        [Authorize]
        public ActionResult Trade(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            JsonResult listMSBO = new JsonResult();
            if (CurrentUserRecord.GroupIdentifier != "4")
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveListMsboRequest request = new RetrieveListMsboRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.Deposit"
                        },
                        Page = new PageRecord
                        {
                            PageSize = 10000,
                            PageIndex = 0
                        },
                        CurrentUser = CurrentUserRecord,
                    };
                    var customerRecords = new RetrieveListMsboResponse();
                    customerRecords = serviceClient.Proxy.RetrieveListMsbo(request);
                    var objectItem = customerRecords.Records.Select(x => new
                    {
                        id = x.Dao,
                        text = x.FullName,
                    });
                    listMSBO = Json(objectItem);
                }
            }
            resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer,
                ListMSBO = listMSBO
            };
            return View(vm);
        }
        [Authorize]
        [HttpPost]
        public ActionResult TradeData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSalePerformanceByDaoRequest request = new RetrieveSalePerformanceByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.CashManagementData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSalePerformanceByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveTradeByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SalePerformanceRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select(x => new object[]{
                                                             x.ProductNameQd,
                                                             x.GroupType==2? SMECustomerWebsite.Core.Resources.Resource.NewLbl : SMECustomerWebsite.Core.Resources.Resource.EOP,
                                                             x.month11,
                                                             x.month11b,
                                                             x.month10,
                                                             x.month10b,
                                                             x.month9,
                                                             x.month9b,
                                                             x.month8,
                                                             x.month8b,
                                                             x.month7,
                                                             x.month7b,
                                                             x.month6,
                                                             x.month6b,
                                                             x.month5,
                                                             x.month5b,
                                                             x.month4,
                                                             x.month4b,
                                                             x.month3,
                                                             x.month3b,
                                                             x.month2,
                                                             x.month2b,
                                                             x.month1,
                                                             x.month1b,
                                                             x.month0,
                                                             x.month0b,
                                                             resultCustomer.IndexOf(x)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        #endregion

        #region KPIs view

        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIs(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIDNBQ(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIDNTCBQ(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIKHHD(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult KPICard(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIBadDebt(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIHDBQ(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIBranch(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                RetrieveUserByDaoRequest request = new RetrieveUserByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.Deposit"
                    },
                    Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : dao,
                    CurrentDao = CurrentUserRecord
                };
                var customerRecords = new RetrieveUserResponse();
                customerRecords = serviceClient.Proxy.RetrieveUserByDao(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new UserRecord() : customerRecords.Record;
            }
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult KPIRegion(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
            {
                RetrieveUserByDaoRequest request = new RetrieveUserByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.Deposit"
                    },
                    Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : dao,
                    CurrentDao = CurrentUserRecord
                };
                var customerRecords = new RetrieveUserResponse();
                customerRecords = serviceClient.Proxy.RetrieveUserByDao(request);
                resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new UserRecord() : customerRecords.Record;
            }
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }
        #endregion

        #region KPI data
        [Authorize]
        [HttpPost]
        public ActionResult RawAvgBalLoanData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataDNBQByDaoRequest request = new RetrieveRawDataDNBQByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawAvgBalLoanData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataDNBQByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataDNBQByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataDNBQRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
              {
                  sEcho = pageRequest.Echo,
                  iTotalRecords = customerRecords.Page.TotalRecords,
                  sColumns = pageRequest.ColumnNames,
                  aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             x.Acctno,
                                                             x.AvgbalQd.ToString("##,##0.00"),
                                                             x.ProductGroup,
                                                             x.DaoCif,
                                                             x.DaoSP,
                                                             x.Note,
                                                             x.IssueDate.HasValue? x.IssueDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty
                    }).ToList()
              };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RawAvgBalDepositData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataDNBQByDaoRequest request = new RetrieveRawDataDNBQByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawAvgBalDepositData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataDNBQByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataHDBQByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataDNBQRecord>() : customerRecords.Records;

                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             x.Acctno,
                                                             x.AvgbalQd.ToString("##,##0.00"),
                                                             x.ProductGroup,
                                                             x.DaoCif,
                                                             x.DaoSP,
                                                             x.Note,
                                                             x.IssueDate.HasValue? x.IssueDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty,
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult RawAvgBalLoanUnsecuredData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataDNBQByDaoRequest request = new RetrieveRawDataDNBQByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawAvgBalLoanUnsecuredData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataDNBQByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataDNTCBQByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataDNBQRecord>() : customerRecords.Records;


                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             x.Acctno,
                                                             x.AvgbalQd.ToString("##,##0.00"),
                                                             x.ProductGroup,
                                                             x.DaoCif,
                                                             x.DaoSP,
                                                             x.Note,
                                                             x.IssueDate.HasValue? x.IssueDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty,
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RawbBadbankData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataDNBQByDaoRequest request = new RetrieveRawDataDNBQByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawbBadbankData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataDNBQByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataBadbankByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataDNBQRecord>() : customerRecords.Records;


                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             x.Acctno,
                                                             x.AvgbalQd.ToString("##,##0.00"),
                                                             x.ProductGroup,
                                                             x.DaoCif,
                                                             x.DaoSP,
                                                             x.Note,
                                                             x.IssueDate.HasValue? x.IssueDate.Value.ToString("dd/MM/yyyy", CultureInfo.InvariantCulture):string.Empty,
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RawCardData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataCardByDaoRequest request = new RetrieveRawDataCardByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawCardData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataCardByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataCardByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataCardRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.Cif,
                                                             x.CusName,
                                                             x.ContractNumber,
                                                             x.ContractStatus,
                                                             x.CardNumber,
                                                             x.CardStatus,
                                                             x.CardType,
                                                             x.DaoCif,
                                                             x.DaoSP,
                                                             x.Note
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RawCustomerActiveData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataKHHDByDaoRequest request = new RetrieveRawDataKHHDByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawCustomerActiveData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataKHHDByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataKHHDByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataKhhdRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.LastYear,
                                                             x.BanGiao,
                                                             x.NhanBanGiao,
                                                             x.SoLuyKe,
                                                             x.SoKhhdTangThem
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult KPIData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSaleKpiByDaoRequest request = new RetrieveSaleKpiByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.KPIData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveSaleKpiByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleKpiByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SaleKPIRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                 {
                     sEcho = pageRequest.Echo,
                     iTotalRecords = customerRecords.Page.TotalRecords,
                     sColumns = pageRequest.ColumnNames,
                     aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.KpiGroup,
                                                             (index + 1).ToString(),
                                                             x.KpiName,
                                                             x.KpiUnit,
                                                             x.KpiWeight,
                                                             x.Target.ToString("##,##0.00"),
                                                             x.Act.ToString("##,##0.00"),
                                                             x.Real.ToString("p1"),
                                                             string.IsNullOrEmpty(x.Url)?string.Empty : x.Url
                    }).ToList()
                 };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult KpiMsboBranchData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveKpiMsboRawDailyByDaoRequest request = new RetrieveKpiMsboRawDailyByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.KpiMsboBranchData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveKpiMsboRawDailyByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveKpiMsboRawDailyByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<KpiMsboRawDailyRecord>() : customerRecords.Records;
                pageResponse = new DataTableResponse
                 {
                     sEcho = pageRequest.Echo,
                     iTotalRecords = customerRecords.Page.TotalRecords,
                     sColumns = pageRequest.ColumnNames,
                     aaData = resultCustomer.Select((x, index) => new object[]{
                                                            x.Dao,
                                                            x.SaleName,
                                                            x.AvgbalLoanTarget.ToString("##,##0.00"),
                                                            x.AvgbalLoanAct.ToString("##,##0.00"),
                                                            x.AvgbalLoanReal.ToString("p1"),
                                                            x.AvgbalDepositTarget.ToString("##,##0.00"),
                                                            x.AvgbalDepositAct.ToString("##,##0.00"),
                                                            x.AvgbalDepositReal.ToString("p1"),
                                                            x.NewnplLoanTarget.ToString("##,##0.00"),
                                                            x.NewnplLoanAct.ToString("##,##0.00"),
                                                            x.NewnplLoanReal.ToString("p1"),
                                                            x.AvgbalUplLoanTarget.ToString("##,##0.00"),
                                                            x.AvgbalUplLoanAct.ToString("##,##0.00"),
                                                            x.AvgbalUplLoanReal.ToString("p1"),
                                                            x.CustomerTarget.ToString("##,##0.00"),
                                                            x.CustomerAct.ToString("##,##0.00"),
                                                            x.CustomerReal.ToString("p1"),
                                                            x.CreditCardTarget.ToString("##,##0.00"),
                                                            x.CreditCardAct.ToString("##,##0.00"),
                                                            x.CreditCardReal.ToString("p1"),
                                                            x.KpiRound
                    }).ToList()
                 };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult KPIRankBranch(DataTablesPageRequest pageRequest, string branch = null)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveRankKpiByBranchRequest request = new RetrieveRankKpiByBranchRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.KPIRankBranch"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    Branch = CurrentUserRecord.Branch
                };
                var customerRecords = new RetrieveRankKpiByBranchResponse();
                customerRecords = serviceClient.Proxy.RetrieveRankKpiByBranch(request);
                var resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new RankKpiRecord() : customerRecords.Record;
                List<KpiRankBranchViewModel> viewModel = new List<KpiRankBranchViewModel>();
                viewModel.Add(new KpiRankBranchViewModel
                {
                    Index = resultCustomer.Yearmonth.ToString(),
                    Score = resultCustomer.TotalScoreMonth.ToString("p1"),
                    Rank = GetEnumDescription((KPIRankType)resultCustomer.RankingMonth)
                });
                viewModel.Add(new KpiRankBranchViewModel
                {
                    Index = resultCustomer.Yearmonth.ToString(),
                    Score = resultCustomer.TotalScoreAccumulated.ToString("p1"),
                    Rank = GetEnumDescription((KPIRankType)resultCustomer.RankingAccumulated)
                });

                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = 10,
                    sColumns = pageRequest.ColumnNames,
                    aaData = viewModel.Select((x, index) => new object[]{
                                                             x.Index,
                                                             x.Score,
                                                             x.Rank
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult KPIRegionData(DataTablesPageRequest pageRequest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveSaleKpiByDaoRequest request = new RetrieveSaleKpiByDaoRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.KPIRegionData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord
                };
                var customerRecords = new RetrieveSaleKpiByDaoResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleKpiRegion(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<SaleKPIRecord>() : customerRecords.Records;
                Random rnd = new Random();
                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.KpiGroup,
                                                             (index + 1).ToString(),
                                                             x.KpiName,
                                                             x.KpiUnit,
                                                             x.KpiWeight,
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100),
                                                             rnd.Next(100)
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }

        #endregion

        #region Sale Contest

        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContest()
        {
            ProductHandbookViewModel vm = new ProductHandbookViewModel();
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveSaleContestProgramRequest request = new RetrieveSaleContestProgramRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProduct"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                };
                var customerRecords = new RetrieveSaleContestProgramResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleContestProgram(request);
                vm.productCat = (customerRecords == null || customerRecords.Records == null) ? new List<ProductHandbookCategoryRecord>() : customerRecords.Records;
            }
            return View(vm);
        }


        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContestRanking(string salecontest)
        {
            int salecontestid;
            if (int.TryParse(salecontest, out salecontestid))
            {
                SaleContestRankingViewModel vm = new SaleContestRankingViewModel
                {
                    DaoInfo = CurrentUserRecord
                };
                SaleContestProgramRecord record;
                if (CheckSaleContestId(out record, salecontestid))
                {
                    vm.SaleContestRecord = record;
                    return View(vm);
                }
            }
            return Redirect("~/SalePerformance/SaleContest");
        }


        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContestRankingBranch(string salecontest)
        {
            int salecontestid;
            if (int.TryParse(salecontest, out salecontestid))
            {
                SaleContestRankingViewModel vm = new SaleContestRankingViewModel
                {
                    DaoInfo = CurrentUserRecord
                };
                SaleContestProgramRecord record;
                if (CheckSaleContestId(out record, salecontestid))
                {
                    vm.SaleContestRecord = record;
                    return View(vm);
                }
            }
            return Redirect("~/SalePerformance/SaleContest");
        }

        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContestRankingRegion(string salecontest)
        {
            int salecontestid;
            if (int.TryParse(salecontest, out salecontestid))
            {
                SaleContestRankingViewModel vm = new SaleContestRankingViewModel
                {
                    DaoInfo = CurrentUserRecord
                };
                SaleContestProgramRecord record;
                if (CheckSaleContestId(out record, salecontestid))
                {
                    vm.SaleContestRecord = record;
                    return View(vm);
                }
            }
            return Redirect("~/SalePerformance/SaleContest");
        }
        [CustomActionFilter]
        [Authorize]
        public ActionResult SaleContestRawDataBilAward(string dao)
        {
            UserRecord resultCustomer = GetUserByDao(dao);
            DepositViewModel vm = new DepositViewModel
            {
                Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : resultCustomer.Dao,
                DaoInfo = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord : resultCustomer
            };
            return View(vm);
        }

        [Authorize]
        [HttpPost]
        public ActionResult RawDataBilAwardData(DataTablesPageRequest pageRequest, string dao)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                FilterRecord filter = new FilterRecord();
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        break;
                    case "2":
                        break;
                    case "3":
                        break;
                    case "4":
                        break;
                    default:
                        break;
                }
                RetrieveRawDataBilAwardRequest request = new RetrieveRawDataBilAwardRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.RawDataBilAwardData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    Dao = string.IsNullOrEmpty(dao) ? CurrentUserRecord.Dao : dao,
                };
                var customerRecords = new RetrieveRawDataBilAwardResponse();
                customerRecords = serviceClient.Proxy.RetrieveRawDataBilAwardByDao(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RawDataBilAwardRecord>() : customerRecords.Records;


                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                             x.CheckTime,
                                                             x.Cif,
                                                             x.CustomerName,
                                                             x.Acctno,
                                                             x.ChuongTrinhSp,
                                                             (x.Duno* 1e9).ToString("##,##0.00"),
                                                             x.XetThuongMsboYn,
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }


        [Authorize]
        [HttpPost]
        public ActionResult SaleContestRankingMSBOData(DataTablesPageRequest pageRequest, string salecontest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveRankingSaleContestMSBORequest request = new RetrieveRankingSaleContestMSBORequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.SaleContestRankingMSBOData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    SaleProgramId = salecontest
                };
                var customerRecords = new RetrieveRankingSaleContestMSBOResponse();
                customerRecords = serviceClient.Proxy.RetrieveRankingSaleContestMSBO(request);

                string urlRawData = string.Empty;
                switch (salecontest)
                {
                    case "1":
                        urlRawData = "/SalePerformance/SaleContestRawDataAutoAward";
                        urlRawData = "";
                        break;
                    case "2":
                        urlRawData = "/SalePerformance/SaleContestRawDataBilAward";
                        break;
                    case "3":
                        urlRawData = "/SalePerformance/SaleContestRawDataSecuredAward";
                        urlRawData = "";
                        break;
                    default:
                        break;
                }

                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RankingSaleContestMSBORecord>() : customerRecords.Records;
                switch (CurrentUserRecord.GroupIdentifier)
                {
                    case "1":
                        pageResponse = new DataTableResponse
                                        {
                                            sEcho = pageRequest.Echo,
                                            iTotalRecords = customerRecords.Page.TotalRecords,
                                            sColumns = pageRequest.ColumnNames,
                                            aaData = resultCustomer.Select((x, index) => new object[]{
                                                                                        x.XepHang == -1 ? "...":x.Region.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.BranchNameSme.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.Dao.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.DaoName.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                                                                                        x.isDetail? urlRawData:string.Empty
                                                }).ToList()
                                        };
                        break;
                    case "2":
                        pageResponse = new DataTableResponse
                                        {
                                            sEcho = pageRequest.Echo,
                                            iTotalRecords = customerRecords.Page.TotalRecords,
                                            sColumns = pageRequest.ColumnNames,
                                            aaData = resultCustomer.Select((x, index) => new object[]{
                                                                                        x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.BranchNameSme.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.Dao.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.DaoName.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                                                                                        x.isDetail? urlRawData:string.Empty
                                                }).ToList()
                                        };
                        break;
                    case "3":
                        pageResponse = new DataTableResponse
                                        {
                                            sEcho = pageRequest.Echo,
                                            iTotalRecords = customerRecords.Page.TotalRecords,
                                            sColumns = pageRequest.ColumnNames,
                                            aaData = resultCustomer.Select((x, index) => new object[]{
                                                                                        x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.Dao.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.DaoName.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                                                                                        x.isDetail? urlRawData:string.Empty
                                                }).ToList()
                                        };
                        break;
                    case "4":
                        pageResponse = new DataTableResponse
                                        {
                                            sEcho = pageRequest.Echo,
                                            iTotalRecords = customerRecords.Page.TotalRecords,
                                            sColumns = pageRequest.ColumnNames,
                                            aaData = resultCustomer.Select((x, index) => new object[]{
                                                                                        x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.Dao.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.DaoName.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                                                        x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                                                                                        x.isDetail? urlRawData:string.Empty
                                                }).ToList()
                                        };
                        break;
                    default:
                        break;
                }
            }
            return Json(pageResponse);
        }

        [Authorize]
        [HttpPost]
        public ActionResult SaleContestRankingBranchData(DataTablesPageRequest pageRequest, string salecontest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveRankingSaleContestMSBORequest request = new RetrieveRankingSaleContestMSBORequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.SaleContestRankingBranchData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    SaleProgramId = salecontest
                };
                var customerRecords = new RetrieveRankingSaleContestMSBOResponse();
                customerRecords = serviceClient.Proxy.RetrieveRankingSaleContestBranch(request);

                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RankingSaleContestMSBORecord>() : customerRecords.Records;

                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                            x.XepHang == -1 ? "...":x.GroupRegion.ToString(),
                                                            x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                            x.XepHang == -1 ? "...":x.BranchNameSme.ToString(),
                                                            x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                            x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        [Authorize]
        [HttpPost]
        public ActionResult SaleContestRankingRegionData(DataTablesPageRequest pageRequest, string salecontest)
        {
            DataTableResponse pageResponse = null;
            using (ServiceClient<ISalePerformance> serviceClient = new ServiceClient<ISalePerformance>())
            {
                RetrieveRankingSaleContestMSBORequest request = new RetrieveRankingSaleContestMSBORequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.SaleContestRankingRegionData"
                    },
                    Page = new PageRecord
                    {
                        PageSize = pageRequest.DisplayLength,
                        PageIndex = pageRequest.DisplayLength > 0 ? pageRequest.DisplayStart / pageRequest.DisplayLength : 0
                    },
                    CurrentUser = CurrentUserRecord,
                    SaleProgramId = salecontest
                };
                var customerRecords = new RetrieveRankingSaleContestMSBOResponse();
                customerRecords = serviceClient.Proxy.RetrieveRankingSaleContestRegion(request);
                var resultCustomer = (customerRecords == null || customerRecords.Records == null) ? new List<RankingSaleContestMSBORecord>() : customerRecords.Records;

                pageResponse = new DataTableResponse
                {
                    sEcho = pageRequest.Echo,
                    iTotalRecords = customerRecords.Page.TotalRecords,
                    sColumns = pageRequest.ColumnNames,
                    aaData = resultCustomer.Select((x, index) => new object[]{
                                                            x.XepHang == -1 ? "...":x.GroupRegion.ToString(),
                                                            x.XepHang == -1 ? "...":x.XepHang.ToString(),
                                                            x.XepHang == -1 ? "...":x.Region.ToString(),
                                                            x.XepHang == -1 ? "...":x.XetthuongYn.ToString(),
                                                            x.XepHang == -1 ? "...":x.NetDuno.ToString("##,##0.00"),
                    }).ToList()
                };
            }
            return Json(pageResponse);
        }
        #endregion

        #region Helper method
        private bool CheckSaleContestId(out SaleContestProgramRecord saleContestRecord, int saleContestId)
        {
            bool result = false;
            using (ServiceClient<IProductHandbook> serviceClient = new ServiceClient<IProductHandbook>())
            {
                RetrieveSaleContestInfoBySaleContestIdRequest request = new RetrieveSaleContestInfoBySaleContestIdRequest
                {
                    Header = new RequestHeaderRecord
                    {
                        ApplicationKey = SystemConfiguration.ApplicationKey,
                        CallerName = "SMEsCustomer360Degree.Controllers.ProductHandbookController.ByProduct"
                    },
                    CurrentUser = CurrentUserRecord,
                    Lang = CurrentLang,
                    SaleContestId = saleContestId
                };
                var customerRecords = new RetrieveSaleContestInfoBySaleContestIdResponse();
                customerRecords = serviceClient.Proxy.RetrieveSaleContestInfoBySaleContestId(request);
                result = (customerRecords != null && customerRecords.Record != null);
                saleContestRecord = (customerRecords == null || customerRecords.Record == null) ? new SaleContestProgramRecord() : customerRecords.Record;
            }
            return result;
        }

        private UserRecord GetUserByDao(string dao)
        {
            UserRecord resultCustomer = new UserRecord();
            if (CurrentUserRecord.GroupIdentifier.Equals("4"))
            {
                resultCustomer = CurrentUserRecord;
            }
            else
            {
                using (ServiceClient<IAuthentication> serviceClient = new ServiceClient<IAuthentication>())
                {
                    RetrieveUserByDaoRequest request = new RetrieveUserByDaoRequest
                    {
                        Header = new RequestHeaderRecord
                        {
                            ApplicationKey = SystemConfiguration.ApplicationKey,
                            CallerName = "SMEsCustomer360Degree.Controllers.SalePerformanceController.GetUserByDao"
                        },
                        Dao = (string.IsNullOrEmpty(dao) || dao == "khong co DAO") ? CurrentUserRecord.Dao : dao,
                        CurrentDao = CurrentUserRecord
                    };
                    var customerRecords = new RetrieveUserResponse();
                    customerRecords = serviceClient.Proxy.RetrieveUserByDao(request);
                    resultCustomer = (customerRecords == null || customerRecords.Record == null) ? new UserRecord() : customerRecords.Record;
                }
            }
            return resultCustomer;
        }
        #endregion


    }
}
